/*
 * Copyright Owen Watson 2009
 * 
 * This file is part of ArtNotes.
 *
 * ArtNotes is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.

 * ArtNotes is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with ArtNotes.  If not, see <http://www.gnu.org/licenses/>.
*/

using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using MonoTouch.Foundation;
using MonoTouch.UIKit;
using System.Drawing;

namespace ArtNotes
{

	/// <summary>
	///  This displays all the tags which are available.
	///  It also needs to be refactored, as it's gotten untidy.
	/// </summary>

	public class ANImageTag : UIScrollView
	{
		
		float labelPosLeft = 20;
		float labelHeight = 30;
		float labelWidth = 100;
		float switchPosLeft = 200;
		
		float startTopPos = 70;
		float newStartTopPos = 0;
		float controlGap = 40;
		
		private ANDatabase anDB;
		private Image image;
		private Tag[] imageTags;
		private Tag[] notImageTags;
		
		private ArrayList tagDisplay;
		
		private tagLayout newTag;
		struct tagLayout
		{
			public UILabel tagLabel;
			public UISwitch tagSwitch;
		}

		private UITextField newTagField;
		private UIImageView newTagFieldOutline;
		private UIButton addTagButton;
		
		private int tagCount = 0;
		
		// From the monotouch samples
		private UIButton RoundedButtonType (float xpos, float ypos, string btitle)
		{
			var button = UIButton.FromType (UIButtonType.RoundedRect);
			button.Frame = new RectangleF (xpos, ypos, 75f, 30f);
			button.BackgroundColor = UIColor.Clear;
			button.SetTitle (btitle, UIControlState.Normal);
			button.SetTitleColor (UIColor.Black, UIControlState.Normal);

			button.Tag = 1;
			// To support reusable cells
			return button;
		}
		
		public ANImageTag (Image img)
		{
			this.ContentSize = new SizeF(320f,370f);
			this.Frame = new RectangleF (0, 0, 320, 370);
			this.ClipsToBounds = true;
			this.ScrollEnabled = true;
			this.AlwaysBounceVertical = true;
			this.Bounds = this.Frame;
			//adjust bounds...?
			this.ShowsVerticalScrollIndicator = true;
			this.DirectionalLockEnabled = true;
			
			
			this.BackgroundColor = UIColor.FromPatternImage(UIImage.FromFile("Images/notepaper.png"));
			
			UILabel title = new UILabel(new RectangleF(100,5,80,40));
			title.Text = "Tags";
			title.BackgroundColor = UIColor.Clear;
			title.Font = UIFont.FromName("Marker Felt", 40);
			this.AddSubview(title);
			
			this.image = img;
			anDB = AN_APP_OPTIONS.AN_DATABASE;
			
			imageTags = anDB.ListTagsForImage(image).ToArray();
			notImageTags = anDB.ListAllTags().ToArray();
			
			this.addTagsToView(imageTags,notImageTags);
			
			newTagFieldOutline = new UIImageView(new RectangleF(10,45,200,55));
			newTagFieldOutline.Image = UIImage.FromFile("Images/text_box_blue.png");
			newTagFieldOutline.BackgroundColor = UIColor.Clear;
			this.AddSubview(newTagFieldOutline);
			
			newTagField = new UITextField(new RectangleF (20f,60f,180f,30f));
			newTagField.BorderStyle = UITextBorderStyle.None;//RoundedRect;
			newTagField.BackgroundColor = UIColor.Clear;
			newTagField.ShouldReturn = newTagFieldInputHandler;
			newTagField.Font = UIFont.FromName ("Marker Felt", 20);
			newTagField.TextColor = UIColor.DarkGray;
			this.AddSubview(newTagField);
			
			//addTagButton = RoundedButtonType(210,20,"Add");
			addTagButton = ImageButtonType(new RectangleF(210,60,50,30),UIImage.FromFile("Images/button_add.png"),UIImage.FromFile("Images/button_add.png"),"Add");
			addTagButton.TouchDown += addButtonPressed;
			this.AddSubview(addTagButton);
		}
		
		private void addTagsToView(Tag[] imageTags, Tag[] notImageTags)
		{
			float scrollContentSize = 0;
			tagDisplay = new ArrayList ();
			
			newStartTopPos = startTopPos;
			
			for (int i = 0; i < imageTags.Length; i++)
			{
				newStartTopPos = newStartTopPos + controlGap;
				
				newTag.tagLabel = new UILabel (
									new RectangleF (labelPosLeft, newStartTopPos, labelWidth, labelHeight)
									) {
										Text = imageTags[i].TagName
									};
				newTag.tagLabel.Font = UIFont.FromName ("Marker Felt", 24);
				newTag.tagLabel.BackgroundColor = UIColor.Clear;
				newTag.tagSwitch = new UISwitch (
									new RectangleF (switchPosLeft, newStartTopPos, labelWidth, labelHeight)
									);
				newTag.tagSwitch.Tag = i;
				newTag.tagSwitch.On = true;
				newTag.tagSwitch.ValueChanged += UISwitchChangeHandler;
				
				tagDisplay.Add (newTag);
			}
			
			bool tagFound = false;
			// hmm....
			tagCount = imageTags.Length;
			
			for (int i = 0; i < notImageTags.Length; i++)
				{
					tagFound = false;
					for (int j = 0; j < imageTags.Length; j++)
					{
						if (imageTags[j].TagName.Equals (notImageTags[i].TagName))
						{
							Console.WriteLine("Found Tag : " + imageTags[j].TagName);
							tagFound = true;
							break;
						}
					}
					if (!tagFound)
					{
						Console.WriteLine("Adding Tag : " + notImageTags[i].TagName);
					
						newStartTopPos = newStartTopPos + controlGap;
			
						newTag.tagLabel = new UILabel (
											new RectangleF (labelPosLeft, newStartTopPos, labelWidth, labelHeight)
											) {
												Text = notImageTags[i].TagName
											};
						newTag.tagLabel.Font = UIFont.FromName ("Marker Felt", 24);
						newTag.tagLabel.BackgroundColor = UIColor.Clear;
						newTag.tagSwitch = new UISwitch (
											new RectangleF (switchPosLeft, newStartTopPos, labelWidth, labelHeight)
											);
						newTag.tagSwitch.Tag = tagCount;
						// Below should be done with set state!
						newTag.tagSwitch.On = false;
						newTag.tagSwitch.ValueChanged += UISwitchChangeHandler;
						
						tagDisplay.Add (newTag);
						tagCount++;
					}
			}
			
			for (int i = 0; i < tagDisplay.Count; i++)
			{
				tagLayout tl = (tagLayout)tagDisplay[i];
				this.AddSubview (tl.tagLabel);
				this.AddSubview (tl.tagSwitch);
				
				scrollContentSize = newTag.tagSwitch.Frame.Y + newTag.tagSwitch.Frame.Height;
				
				if(scrollContentSize > 350)
					this.ContentSize = new SizeF(this.ContentSize.Width, (scrollContentSize + controlGap) + 75);
			}
		}
		
		void addButtonPressed(object sender, EventArgs args)
		{
			float scrollContentSize = 0;
			Console.WriteLine("Button Pressed");
			
			tagLayout tmpTag;
			bool tagFound = false;
			for (int i = 0; i< tagDisplay.Count; i++)
			{
				tagFound = false;
				tmpTag = (tagLayout)tagDisplay[i];
				if(tmpTag.tagLabel.Text.Equals(newTagField.Text))
				{
					tagFound = true;
					break;
				}
			}
			if(!tagFound)
			{
				Console.WriteLine("Adding Tag : " + newTagField.Text);
						
				newStartTopPos = newStartTopPos + controlGap;
	
				newTag.tagLabel = new UILabel (
									new RectangleF (labelPosLeft, newStartTopPos, labelWidth, labelHeight)
									) {
										Text = newTagField.Text
									};
				newTag.tagLabel.Font = UIFont.FromName ("Marker Felt", 24);
				newTag.tagLabel.BackgroundColor = UIColor.Clear;
				newTag.tagSwitch = new UISwitch (
									new RectangleF (switchPosLeft, newStartTopPos, labelWidth, labelHeight)
									);
				newTag.tagSwitch.Tag = tagCount;
				// Below should be done with set state!
				newTag.tagSwitch.On = true;
				newTag.tagSwitch.ValueChanged += UISwitchChangeHandler;
				
				tagDisplay.Add (newTag);
				tagCount++;
				tagLayout tl = (tagLayout)tagDisplay[tagCount-1];
				this.AddSubview(tl.tagLabel);
				this.AddSubview(tl.tagSwitch);
				
				scrollContentSize = newTag.tagSwitch.Frame.Y + newTag.tagSwitch.Frame.Height;
				
				if(scrollContentSize > 350)
					this.ContentSize = new SizeF(this.ContentSize.Width, (scrollContentSize + controlGap) + 75);
			}
		}
		
		bool newTagFieldInputHandler(UITextField tf)
		{
			tf.ResignFirstResponder();
			Console.WriteLine("New Tag Field Text: " + tf.Text);
			return true;
		}
		
		void UISwitchChangeHandler (object sender, EventArgs args)
		{
			UISwitch s = (UISwitch)sender;
			Console.WriteLine ("UISwitch " + s.Tag + " Value : " + s.On);
		}
		
		public void saveChanges()
		{
			ArrayList tmpTags = new ArrayList();
			tagLayout tmpTag;
			for (int i = 0; i< tagDisplay.Count; i++)
			{
				tmpTag = (tagLayout)tagDisplay[i];
				if(tmpTag.tagSwitch.On == true)
				{
					tmpTags.Add(new Tag(){TagName = tmpTag.tagLabel.Text});
				}
			}
			Tag[] newTagList = (Tag[])tmpTags.ToArray(typeof(Tag));
			anDB.UpdateTagsForImage(this.image,newTagList);
		}

		private UIButton ImageButtonType (RectangleF frame, UIImage defaultImage, UIImage pressedImage, string title)
		{
			var background = defaultImage;//UIImage.FromFile("Images/btnSearch.png");
			var backgroundPressed = pressedImage;//UIImage.FromFile("Images/btnSearchPressed.png");
			//var frame = frame;
			
			var button = new UIButton(frame);
			var nbackground = background.StretchableImage(12,0);
			var nbackgroundPressed = backgroundPressed.StretchableImage(12,0);
			button.SetBackgroundImage(nbackground,UIControlState.Normal);
			button.SetBackgroundImage(nbackgroundPressed,UIControlState.Highlighted);

			button.SetTitle (title, UIControlState.Normal);
			button.Font = UIFont.FromName("Marker Felt",18);
			button.SetTitleColor (UIColor.White, UIControlState.Normal);
			
			button.Tag = 1;
			return button;
		}
	}
}
